<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .page-header {
            font-size: 24px;
            color: #656565;
        }

        .item {
            width: 40px;
            height: 5px;
            border: 5px solid #4444;
            float: left;
            padding: 50px;
            margin-left: 4px;
        }
    </style>
</head>

<body>
    <div class="container">
        <div class="page-header">点击切换颜色</div>
        <div class="item"></div>
        <div class="item"></div>
        <div class="item"></div>
    </div>

    <script>
        //获取div元素对象
        let items = document.getElementsByClassName('item');
        //遍历并绑定事件
        for (let i = 0; i < items.length; i++) {
            items[i].onclick = function () {
                console.log(i);
                //修改当前元素的背景颜色
                // this.style.background = 'pink';
                items[i].style.background = 'pink';
            }
        }

        // console.log(window.i);

    </script>



</body>

</html>